package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/19 10:50
 * @description：https://leetcode-cn.com/problems/contains-duplicate/
 */
public class 存在重复元素III {

    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.containsNearbyAlmostDuplicate(new int[]{1,5,9,1,5,9},2,3));
    }

    class Solution {
        public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
            PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
            for (int i = 0; i < nums.length; i++) {
                if (i > k) {
                    queue.remove(nums[i - k - 1]);
                }
                Integer min = queue.peek();
                if (min != null && nums[i] - min <= t) {
                     return true;
                }
                queue.offer(nums[i]);

            }
            return false;
        }
    }
}
